package com.licode.utils.unilite.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密相关工具类
 *
 * @author licode
 * @version 1.0
 */
public class EncryptUtil {
    public static String encryptMd5(String data) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        md.update(data.getBytes());
        byte[] encryptData = md.digest();
        int i;
        StringBuilder buf = new StringBuilder();
        for (byte encryptDatum : encryptData) {
            i = encryptDatum;
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(i));
        }
        return buf.toString();
    }
}
